package com.kedeng.yangmuyi.common.util;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * @ClassName: DateUtils
 * @Description: Customer DateUtils for this project and extends from apache
 *               jar.
 * @author rqian
 * @date Aug 9, 2013 3:26:59 PM
 * 
 */
public class KDDateUtils extends DateUtils {
    public static final char   TIME_DELIMITER_CHAR         = ':';
    public static final String TIME_DELIMITER              = String.valueOf(TIME_DELIMITER_CHAR);
    public static final String DEFAULT_DATE_FORMAT_PATTERN = "dd/MM/yyyy";



    /**
     * convert date to calendar.
     * 
     * @param pDate
     * @return
     */
    public static Calendar converToCalendar(Date date) {
        Calendar lCalendar = Calendar.getInstance();
        lCalendar.setTime(date);
        return lCalendar;
    }



    /**
     * split hour and minutes by ":".
     * 
     * @param time
     * @return
     */
    public static int[] splitHourMinutes(String time) {
        String[] temp = time.split(TIME_DELIMITER);
        return new int[] { Integer.parseInt(temp[0]), Integer.parseInt(temp[1]) };
    }



    /**
     * get max date in date array.
     * 
     * @param dates
     * @return
     */
    public static Date getMaxDate(Date[] dates) {
        if (dates == null || dates.length == 0) {
            return null;
        }
        sortDates(dates);
        // find this is ascending or descending
        return dates[dates.length - 1];
    }



    /**
     * sort date array.
     * 
     * @param dates
     */
    public static void sortDates(Date[] dates) {
        Arrays.sort(dates);
    }



    /**
     * format date to default date format.
     * 
     * @param pDate
     * @param pattern
     * @return
     */
    public static String formatDate(Date pDate, String... pattern) {
        String result = null;
        if (pDate != null) {
            String lPattern = DEFAULT_DATE_FORMAT_PATTERN;
            if (!KDCollectionUtils.isEmpty(pattern)) {
                lPattern = pattern[0];
            }
            SimpleDateFormat lFormat = new SimpleDateFormat(lPattern, Locale.getDefault());
            result = lFormat.format(pDate);
        }
        return result;
    }



    /**
     * 
     * check time format is correct, it should be standar time format. <br>
     * For example: 23:25 or 4:56
     * 
     * @param time
     * @return
     */
    public static boolean checkTimeFormat(String time) {
        boolean result = false;
        if (StringUtils.isBlank(time)) {
            return result;
        }
        String regex = "^(([0-1]?[0-9])|([2][0-3])):([0-5][0-9])(:([0-5]?[0-9]))?$";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(time);
        if (matcher.find()) {
            result = true;
        }
        return result;
    }
}
